package com.csg.controller;

import com.csg.entity.SysUser;
import com.csg.model.Result;
import com.csg.model.StatusCode;
import com.csg.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *     系统用户
 *     前端控制器
 * </p>
 *
 * @author GeorgeChan 2020/2/6 21:36
 * @version 1.0
 * @since jdk1.8
 */
@RestController
@RequestMapping("/api/system/user/")
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    private final ISysUserService sysUserService;

    public UserController(ISysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 添加用户
     * @param user 用户对象实体
     * @return 操作结果
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Result add(@RequestBody SysUser user) {
        boolean save = sysUserService.save(user);
        if (!save) {
            return new Result(false, StatusCode.ERROR, "保存用户失败！");
        }
        return new Result(true, StatusCode.OK, "操作成功！");
    }

    /**
     * 分页获取用户列表
     * @param query 查询条件
     * @param pagenum
     * @param pagesize
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Result userList(@RequestParam(value = "query", defaultValue = "") String query,
                           @RequestParam(value = "pagenum", defaultValue = "2")int pagenum,
                           @RequestParam(value = "pagesize", defaultValue = "1")int pagesize) {
        PageInfo<SysUser> pageInfo = sysUserService.findUserListPage(query, pagenum, pagesize);
        return new Result(true, StatusCode.OK, "操作成功！", pageInfo);
    }

    /**
     * 更新用户状态
     * @param id
     * @param state
     * @return
     */
    @RequestMapping("{id}/state/{state}")
    public Result changeUserStatus(@PathVariable(value = "id") String id,
                                   @PathVariable(value = "state") Boolean state) {
        SysUser user = sysUserService.getById(id);
        user.setAvailable(state == true ? 0 : 1);
        boolean flag = sysUserService.updateById(user);
        if (flag) {
            return new Result(true, StatusCode.OK, "操作成功！");
        }
        return new Result(false, StatusCode.ERROR, "操作失败！");
    }

    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public Result get(@PathVariable(value = "id") String id) {
        SysUser byId = sysUserService.getById(id);
        return new Result(true, StatusCode.OK, "获取成功！", byId);
    }

    /**
     * 更新用户信息
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public Result updateUser(@PathVariable(value = "id") String id,@RequestBody SysUser user) {
        SysUser byId = sysUserService.getById(id);
        byId.setMobile(user.getMobile());
        byId.setEmail(user.getEmail());

        boolean b = sysUserService.updateById(byId);
        if (b) {
            return new Result(true, StatusCode.OK, "更新成功！");
        }
        return new Result(true, StatusCode.OK, "更新失败！");
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id")String id) {
        boolean res = sysUserService.removeById(id);
        if (res) {
            return new Result(true, StatusCode.OK, "操作成功！");
        }
        return new Result(true, StatusCode.ERROR, "操作失败！");
    }
}
